package com.ffcs.crmd.tsp.core.netty.client;

import com.ffcs.crmd.tsp.api.netty.INettyRemotingClient;
import com.ffcs.crmd.tsp.core.netty.NettyDecoder;
import com.ffcs.crmd.tsp.core.netty.NettyEncoder;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldPrepender;

/**
 * 
 * 功能说明:netty初始化类，用来加载Handler、解码器等
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SANPSHOT
 *
 */
public class NettyClientInitializer extends ChannelInitializer<SocketChannel> {
    
    private INettyRemotingClient remotingClient;
    
    public NettyClientInitializer(INettyRemotingClient remotingClient) {
        this.remotingClient = remotingClient;
    }
    
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(remotingClient.getDefaultEventExecutorGroup());
        //pipeline.addLast(new IdleStateHandler(0, 0, remotingClient.getClientChannelMaxIdleTimeSeconds()));
        pipeline.addLast(new LengthFieldPrepender(4, false));
        pipeline.addLast(new NettyEncoder());
        pipeline.addLast(new NettyDecoder());
        pipeline.addLast(new NettyConnetManageHandler(remotingClient));
        pipeline.addLast(new NettyClientHandler(remotingClient));
    }
    
}
